<#include '/admin/header.html' />
<link rel="stylesheet" type="text/css" href="${ctx}/adminthemes/version3/plugins/select2/css/select2.min.css"/>
<script type="text/javascript" src="${ctx}/adminthemes/version3/plugins/select2/js/select2.min.js"></script>
<style>
.__GCD__content .__GCD__input input{
height:30px;
line-height:30px;
padding:4px 10px;
}
</style>
<div class="admin-main" id="__cat_box__" style="margin-top: 30px;">
    <script type="text/javascript" src="${ctx}/statics/e_tools/js/jquery.goodsCatsEdit.js"></script>
    <script>
        var ctx = '${ctx}';
        var _goodsCatEdit = $('#__cat_box__').goodsCatsEdit({
        	api : "/shop/admin/cat/get-list-by-parentid-json.do",
            canEdit  : true,
            addCat   : function (cat) {
                $.ajax({
                    url: ctx + '/shop/admin/cat/add.do',
                    success: function (html) {
                        layer.open({
                            type: 1,
                            title: '添加分类',
                            area: ['600px', 'auto'],
                            content: html,
                            btn: ['确定', '取消'],
                            success: function (layero, index) {
                                cat.leave === 1 && (function () {
                                    var parent_dom = layero.find("input[name='parent_id']");
                                    parent_dom.val(0);
                                    parent_dom[0].readOnly = true;
                                })();

                                cat.leave !== 1 && (function () {
                                    layero.find('#cat_parent').replaceWith('<select name="parent_id" id="cat_parent" class="layui-input" style="width: 235px;"></select>');
                                    layero.find('#cat_parent').html('<option value="'+ cat.parent_data['data']['id'] +'">'+ cat.parent_data['data']['text'] +'</option>');
                                    layero.find('#cat_parent').select2({ data: cat.parent_data.datas });
                                })();

                                layero.find('#goods_type').replaceWith('<select name="type_id" id="goods_type" class="layui-input" style="width: 235px;"></select>');
                                $.ajax({
                                    url: ctx + '/shop/admin/cat/type-list-json.do',
                                    type: 'GET',
                                    dataType: 'json',
                                    success: function (res) {
                                        if(res.result === 0) return;
                                        layero.find('#goods_type').select2({ data: res })
                                    }
                                })
                            },
                            yes: function (index, layero) {
                                var _name = layero.find('#name').val(), _sort = layero.find('#sort').val();
                                if(!_name || !_sort || isNaN(_sort)){
                                    layer.msg('表单填写有误，请核对！', { icon: 2 });
                                }else {
                                    layero.find('#addForm').ajaxSubmit({
                                        url: ctx + '/shop/admin/cat/save-add.do',
                                        type: 'POST',
                                        success: function (res) {
                                            if(res.result === 1){
                                                layer.msg('保存成功！', { icon: 1 });
                                                _goodsCatEdit.refreshData();
                                                layer.close(index);
                                            }else {
                                                layer.msg(res.message, { icon: 5 });
                                            }
                                        },error: function () {
                                            layer.msg('出现错误，请重试！', { icon: 5 });
                                        }
                                    });
                                }
                            },
                            btn2: function(index, layero){
                              
                            }
                        })
                    }
                })
            },
            editCat  : function (cat) {
                $.ajax({
                    url: ctx + '/shop/admin/cat/edit.do',
                    data: { cat_id: cat.id },
                    success: function (html) {
                        layer.open({
                            type: 1,
                            title: '编辑分类',
                            area: ['600px', '445px'],
                            content: html,
                            btn: ['确定', '取消'],
                            success: function (layero, index) {
                                cat.leave !== 1 && (function () {
                                    layero.find('#cat_parent').replaceWith('<select name="parent_id" id="cat_parent" class="layui-input" style="width: 235px;"></select>');
                                    layero.find('#cat_parent').html('<option value="'+ cat.parent_data['data']['id'] +'">'+ cat.parent_data['data']['text'] +'</option>');
                                    layero.find('#cat_parent').select2({ data: cat.parent_data['datas'] });
                                })();
                                var _type_id = parseInt(layero.find('#goods_type').val());
                                layero.find('#goods_type').replaceWith('<select name="type_id" id="goods_type" class="layui-input" style="width: 235px;"></select>');
                                $.ajax({
                                    url: ctx + '/shop/admin/cat/type-list-json.do',
                                    type: 'GET',
                                    dataType: 'json',
                                    success: function (res) {
                                        if(res.result === 0) return;
                                        for(var i = 0; i < res.length; i++){
                                            if(res[i]['id'] === _type_id){
                                                layero.find('#goods_type').html('<option value="'+ _type_id +'">'+ res[i]['text'] +'</option>');
                                                break;
                                            }
                                        }

                                        layero.find('#goods_type').select2({ data: res })
                                    }
                                })
                            },
                            yes: function (index, layero) {
                                var _name = layero.find('#name').val(), _sort = layero.find('#sort').val();
                                if(!_name || !_sort || isNaN(_sort)){
                                    layer.msg('表单填写有误，请核对！', { icon: 2 });
                                }else {
                                    layero.find('#editForm').ajaxSubmit({
                                        url: ctx + '/shop/admin/cat/save-edit.do',
                                        type: 'POST',
                                        success: function (res) {
                                            if(res.result === 1){
                                                layer.msg('保存成功！', { icon: 1 });
                                                _goodsCatEdit.refreshData();
                                                layer.close(index);
                                            }else {
                                                layer.msg(res.message, { icon: 5 });
                                            }
                                        },error: function () {
                                            layer.msg('出现错误，请重试！', { icon: 5 });
                                        }
                                    });
                                }
                            },
                            btn2: function(index, layero){
                              
                            }
                        })
                    }
                })
            },
            deleteCat: function (cat) {
                layer.confirm('确定要删除这个分类吗？', {icon: 3, title:'提示'}, function (index) {
                    layer.close(index);
                    var __load = layer.load(2);
                    $.ajax({
                        url: ctx + '/shop/admin/cat/delete.do',
                        data: { cat_id: cat.id },
                        type: 'POST',
                        success: function (res) {
                            layer.close(__load);
                            if(res.result === 1){
                                layer.msg('删除成功！', { icon: 1 });
                                _goodsCatEdit.refreshData();
                            }else {
                                layer.msg(res.message, { icon: 5 });
                            }
                        },
                        error: function () {
                            layer.close(__load);
                            layer.msg('出现错误，请稍候再试！', { icon: 2 });
                        }
                    })
                })
            },
            callback: function (cat) {
                console.log(cat)
            }
        }).init();
    </script>
</div>
<#include '/admin/footer.html' />